from CvPythonExtensions import *
gc = CyGlobalContext()
localtext = CyTranslator()

import GerikSpells
import GerikSpellsUtils

from SdToolkitAdvanced import sdObjectGetVal

from GerikSpells import GerikSpellBase as GSBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSTargetting as GSHandler

from GSResultsPolicies import GSMissionCasterCounter as GSBankBreachResults
from GSResultsPolicies import GSMissionTargetCounter as GSSabotageResults

from GSGraphicsPolicies import GSPlot
from GSTargetInfoPolicies import GSSinglePlotTarget

BANK_BREACH_TIME = 4
SABOTAGE_TIME = 15

# The base class used for both cyber terrorism abilities.
class CyberTerrorism(GSBase, GSGroup, GSHandler):
    def __init__(self):
        GSBase.__init__(self)
        GSGroup.__init__(self)
        
        pPlotTargetPolicy = GSSinglePlotTarget(self.canTargetPlot)
        pPlotTargetPolicy.setRangeMakerCallback(self.getRangePlotList)
        pPlotGraphicsPolicy = GSPlot()
        pPlotGraphicsPolicy.setShowRangeGraphics(True)

        stageList = [
            (pPlotTargetPolicy, pPlotGraphicsPolicy)
        ]

        GSHandler.__init__(self, stageList)

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if pCastingUnit.getUnitType() == gc.getInfoTypeForString("UNIT_WARRIOR"):
            return True

        return False

    def canTargetPlot(self, pCastingGroup, pCastingPlot, pPlot, bVisible):
        if (pPlot.isCity()):
            pCity = pPlot.getPlotCity()
            if (pCity.getOwner() != pCastingGroup.getOwner()):
                return True

        return False

    def getRangePlotList(self, pCastingPlot):
        return GerikSpellsUtils.getRangePlotDistanceList(pCastingPlot, 3)

    def castSpell(self, pTargetInfo):
        self.pushSpellMission(pTargetInfo.getData1(), pTargetInfo.getData2(), pTargetInfo.getData3())

    
class CyberTerrorismBankBreach(CyberTerrorism, GSBankBreachResults):
    def __init__(self):
        CyberTerrorism.__init__(self)

        pPlotTargetPolicy = GSSinglePlotTarget(self.canTargetPlot)
        GSBankBreachResults.__init__(self, pPlotTargetPolicy)

    def getCounterGoal(self, pUnit):
        return BANK_BREACH_TIME

    def continueSpellResultsOnTarget(self, pCastingUnit, pTargetInfo):
        pCity = pTargetInfo.getTarget().getPlotCity()
        pTargetPlayer = gc.getPlayer(pCity.getOwner())
        pRecievingPlayer = gc.getPlayer(pCastingUnit.getOwner())

        # (Population * (UnitLevel + 1)) + (random number 0-24)
        iStolen = (pCity.getPopulation() * (pCastingUnit.getLevel() + 1))+ (3 * gc.getGame().getSorenRandNum(9, "Stolen Credits"))
        iStolen = min(gc.getPlayer(pCity.getOwner()).getGold(), iStolen)

        pTargetPlayer.changeGold(-iStolen)
        pRecievingPlayer.changeGold(iStolen)
                    

        CyEngine().triggerEffect(gc.getInfoTypeForString("EFFECT_LASER_HIT"), pCastingUnit.plot().getPoint())
        szMessage = localtext.getText("TXT_KEY_ABILITY_CYBER_TERRORISM_BANK_BREACH_SUCCESSFUL", (iStolen, pCity.getName()))
        CyInterface().addMessage(pCastingUnit.getOwner(), True, 10, szMessage,
                                 '', 1 , gc.getSpellInfo(self.getSpellNumber()).getButton(), ColorTypes(10),
                                 pCity.getX(),pCity.getY(),True,True)

        return True

    def getUnitHelpText(self, pUnit, bOneLine, bShort):
        iTurnsLeft = self.getCounterGoal(pUnit) - self.getCompletionValue(pUnit)
        if (iTurnsLeft > 0):
            return localtext.getText("TXT_KEY_ABILITY_CYBER_TERRORISM_BANK_BREACH_UNIT_HELP", (iTurnsLeft,))

class CyberTerrorismSabotageEngineeringData(CyberTerrorism, GSSabotageResults):
    def __init__(self):
        CyberTerrorism.__init__(self)

        
        pPlotTargetPolicy = GSSinglePlotTarget(self.canTargetPlot)
        GSSabotageResults.__init__(self, pPlotTargetPolicy)

    def getCounterGoal(self, pTarget):
        return SABOTAGE_TIME

    def continueSpellResultsOnTarget(self, pCastingUnit, pTargetInfo):
        pCity =pTargetInfo.getTarget().getPlotCity()

        iProductionSabotaged = (gc.getGame().getSorenRandNum(9, "Hammers sabotaged") + 1 * 10)
        iFinalProductionSabotaged = min(iProductionSabotaged, pCity.getProduction())
        szMessage = localtext.getText("TXT_KEY_ABILITY_CYBER_TERRORISM_SABOTAGE_SUCCESSFUL",
                               (iFinalProductionSabotaged, pCity.getName()))

        pCity.changeProduction(-iFinalProductionSabotaged)
                        

        CyInterface().addMessage(pCastingUnit.getOwner(), True, 10, szMessage, '', 1,
                                 gc.getSpellInfo(self.getSpellNumber()).getButton(), ColorTypes(10),
                                 pCity.getX(), pCity.getY(), True, True)


        return False

    def getUnitHelpText(self, pUnit, bOneLine, bShort):
        pTargetInfo = self.getUnitsTargetInfo(pUnit)
        if (pTargetInfo is None):
            return ""

        pTarget = pTargetInfo.getTarget()

        iTurnsLeft = self.getCompletionTurnsLeft(pTarget)
        if (iTurnsLeft > 0):
            return localtext.getText("TXT_KEY_ABILITY_CYBER_TERRORISM_SABOTAGE_ENGINEERING_DATA_UNIT_HELP", (iTurnsLeft,))

